import tkinter as tk
from tkinter import filedialog
import os

root = tk.Tk()
root.geometry("500x500")
root.title("dir2xyz")


def choose_dir():
    folder_selected = filedialog.askdirectory()
    print(folder_selected)
    show_info.set(folder_selected)
    #     if not os.path.exists(folder_selected):
    #         return
    file = open(folder_selected+os.sep+os.path.basename(folder_selected)+".txt","w",encoding="utf-8")
    myFiles = os.listdir(folder_selected)
    for imgName in myFiles:
        if(imgName.endswith(".JPG") and len(imgName.split("-"))>=3):
            xyzArr = imgName.replace(".JPG","").split("-")
            line_str = imgName+"    "+xyzArr[0]+"   "+xyzArr[1]+"   "+xyzArr[2]+"\r\n"
            file.write(line_str)
    file.close()
    show_info.set("操作成功！")


button = tk.Button(root, text="Select image dir", command=choose_dir)
button.pack(side="top", pady=50)

show_info = tk.StringVar()
panel = tk.Label(root, text='图片路径:', textvariable=show_info)
panel.pack(side="top", expand="yes", padx="50", pady="50")

root.mainloop()